package com.fausto.learnimprint.mail

import com.fausto.learnimprint.core.model.dto.CommonResult
import com.fausto.learnimprint.core.service.IUserService
import com.fausto.learnimprint.utils.FormatUtil
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*

@Api(tags = ["邮箱API"], basePath = "/mails")
@RestController
@RequestMapping("/mails")
class MailController {

    @Autowired
    lateinit var redisTemplate: RedisTemplate<String, String>

    @Autowired
    lateinit var mailService: MailService

    @Autowired
    lateinit var userService: IUserService

    /**
     *          发送验证邮件
     */
    @ApiOperation(value = "发送验证邮件", notes = "mail 冷却五分钟")
    @PostMapping
    fun sendMail(
        @ApiParam(value = "待绑定邮箱号", example = "1096249048@qq.com")
        @RequestBody mail: String
    ): CommonResult<Any> {
        return if (mail.isBlank() || !FormatUtil.checkMail(mail)) {
            CommonResult.fail("邮箱格式错误")
        } else try {
            //此邮箱五分钟内发送过验证码
            if (mailService.getMailCodeFromRedis(mail) != null) {
                CommonResult.fail("${MailConstant.EXPIRED_TIME} 分钟内不可重发验证码")
            } else {
                //将邮件发送至消息队列
                mailService.sendMail(mail)
                CommonResult.ok("发送成功")
            }
        } catch (e: Exception) {
            CommonResult.fail("操作失败", data = e.message)
        }
    }


    /**
     * 查询邮件发送状态
     *
     * @param mail
     * @return
     */
    @ApiOperation(value = "查询邮件发送状态", notes = "查询邮件发送状态 0 发送中 1 已发送 2 发送失败")
    @PostMapping("/getMailSendState")
    fun getMailSendState(
        @ApiParam(value = "邮箱号", example = "1096249048@qq.com")
        @RequestParam mail: String
    ): CommonResult<Any> {

        return if (mail.isBlank()) {
            CommonResult.fail("邮箱格式错误")
        } else try {
            //获取缓存中的状态
            val value = redisTemplate.opsForValue().get(MailConstant.REDIS_MAIL_KEY_PREFIX + mail)
            if (null != value) {
                val state = value[value.length - 1]
                CommonResult.ok("查询成功", data = state)
            } else {
                CommonResult.fail("邮件不存在")
            }
        } catch (e: Exception) {
            CommonResult.fail("操作失败", data = e.message)
        }
    }

    /**
     * 获取用户绑定的邮箱
     *
     * @return
     */
    @GetMapping
    @PreAuthorize("hasAuthority('USER')")
    @ApiOperation(value = "获取用户绑定的邮箱")
    fun getUserMail(): CommonResult<Any> {
        return try {
            CommonResult.ok("查询成功", data = userService.findUserMail())
        } catch (e: Exception) {
            CommonResult.fail("操作失败", data = e.message)
        }
    }

//    @ApiOperation(value = "改绑邮箱")
//    @PreAuthorize("hasAuthority('USER')")
//    @PostMapping("/updateMail")
//    fun updateMail(
//        @ApiParam("新邮箱号")
//        newMail: String,
//        @ApiParam("旧邮箱号验证码")
//        oldMailCode: String,
//        @ApiParam("新邮箱号验证码")
//        newMailCode: String
//    ): CommonResult<Any> {
//        return if (newMail.isBlank() || oldMailCode.isBlank() ||
//            newMailCode.isBlank() || FormatUtil.checkMail(newMail)){
//            CommonResult.fail("参数错误")
//        }else try {
//
//
//
//        }catch (e: Exception){
//            CommonResult.fail("操作失败", data = e.message)
//        }
//    }
}